package com.skynet.supervision.utils;

import cn.hutool.core.util.ObjectUtil;

import com.google.common.collect.Lists;
import com.skynet.supervision.beans.response.SysDictDataVo;
import com.skynet.supervision.constants.SysConstants;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典工具类
 *
 * @author xuchao
 */
public class DictUtils {
    /**
     * 分隔符
     */
    public static final String SEPARATOR = ",";

    /**
     * 设置字典缓存
     *
     * @param key          参数键
     * @param dictDataList 字典数据列表
     */
    public static void setDictCache(String key, List<SysDictDataVo> dictDataList) {
        CacheUtils.put(getCacheName(), getCacheKey(key), dictDataList);
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     * @return dictData 字典数据列表
     */
    public static List<SysDictDataVo> getDictCache(String key) {
        Object cacheObj = CacheUtils.get(getCacheName(), getCacheKey(key));
        if (ObjectUtil.isNotNull(cacheObj)) {
            return ObjectUtils.cast(cacheObj);
        }
        return null;
    }
//    public static SysDictData getDictValue(String key, String dictCode) {
//        List<SysDictData> dataList = getDictCache(key);
//        for (SysDictData dict : dataList) {
//            if (dictCode.equals(dict.getCode())) {
//                return dict.getValue();
//            }
//        }
//        return null;
//    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param key      字典key
     * @param dictCode 字典Code
     * @return 字典值
     */
    public static String getDictValue(String key, String dictCode) {
        List<SysDictDataVo> dataList = getDictCache(key);
        if (CollectionExtUtils.notEmpty(dataList)) {
            for (SysDictDataVo dict : dataList) {
                if (dictCode.equals(dict.getCode())) {
                    return dict.getValue();
                }
            }
        }
        return null;
    }

    public static SysDictDataVo getDictData(String key, String dictCode) {
        List<SysDictDataVo> dataList = getDictCache(key);
        if (CollectionExtUtils.notEmpty(dataList)) {
            for (SysDictDataVo dict : dataList) {
                if ((Strings.isEmpty(dictCode) && dict.getIsDefault()) || (Strings.notEmpty(dictCode) && dictCode.equals(dict.getCode()))) {
                    return dict;
                }
            }
        }
        return null;
    }

    public static List<SysDictDataVo> getDictDataMulti(String key, String dictCodes) {
        List<String> codeToGet = Arrays.stream(dictCodes.split(SEPARATOR)).collect(Collectors.toList());
        List<SysDictDataVo> res = Lists.newArrayList();
        if (CollectionExtUtils.isEmpty(codeToGet)) {
            return res;
        }
        List<SysDictDataVo> dataList = getDictCache(key);
        if (CollectionExtUtils.notEmpty(dataList)) {
            for (String code : codeToGet) {
                for (SysDictDataVo dict : dataList) {
                    if (code.equals(dict.getCode())) {
                        res.add(dict);
                    }
                }
            }
        }
        return res;
    }

    /**
     * 删除指定字典缓存
     *
     * @param key 字典键
     */
    public static void removeDictCache(String key) {
        CacheUtils.remove(getCacheName(), getCacheKey(key));
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCache() {
        CacheUtils.removeAll(getCacheName());
    }

    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    public static String getCacheName() {
        return SysConstants.SYS_DICT_CACHE;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return SysConstants.SYS_DICT_KEY + configKey;
    }
}
